﻿Imports System.Math

Public Class DataPage
    Public total_records As Integer = 0
    Public start As Integer = 0
    Public scroll_page As Integer = 0
    Public per_page As Integer = 0
    Public total_pages As Integer = 0
    Public current_page As Integer = 0
    Public page_links As String = ""
    Public previous_page As String = ""
    Public next_page As String = ""
    Public first_page As String = ""
    Public last_page As String = ""
    Public pager_url As String = ""
    ' total pages and essential variables
    Public Sub Subtotal_pages(ByVal pager_url As String, ByVal total_records As Integer, ByVal scroll_page As Integer, ByVal per_page As Integer, ByVal current_page As Integer)
        total_records = total_records
        scroll_page = scroll_page
        per_page = per_page
        If Not IsNumeric(current_page) Then
            current_page = 1
        Else
            current_page = current_page
        End If
        If current_page = 1 Then
            start = 0
        Else
            start = (current_page - 1) * per_page
        End If
        total_pages = Ceiling(total_records / per_page)
    End Sub

    ' page links
    Public Sub Subpage_links(ByVal inactive_page_tag As String, ByVal pager_url_last As String, ByVal total_records As Integer)
        Dim loop_start As Integer = 0
        Dim loop_finish As Integer = 0
        If total_pages <= scroll_page Then
            If total_records <= per_page Then
                loop_start = 1
                loop_finish = total_pages
            Else
                loop_start = 1
                loop_finish = total_pages
            End If
        Else
            If current_page < CInt(Floor(scroll_page / 2)) + 1 Then
                loop_start = 1
                loop_finish = scroll_page
            Else
                loop_start = current_page - CInt(Floor(scroll_page / 2))
                loop_finish = current_page + CInt(Floor(scroll_page / 2))
                If loop_finish > total_pages Then
                    loop_finish = total_pages
                End If
            End If
        End If

        If loop_start > 1 Then
            page_links &= "<li>...</li>"
        End If

        For i As Integer = loop_start To loop_finish
            If i = current_page Then
                page_links &= "<li " & inactive_page_tag & ">" & i & "</li>"
            Else
                page_links &= "<li><a href=""" & pager_url & i & pager_url_last & """>" & i & "</a></li>"
            End If
        Next
        If loop_finish > 5 And loop_finish < total_pages Then
            page_links &= "<li>...</li>"
        End If
    End Sub

    ' previous page
    Public Sub Subprevious_page(ByVal previous_page_text As String, ByVal pager_url_last As String)
        If current_page > 1 Then
            previous_page = "<li class=""separight""><a href=""" & pager_url & (current_page - 1) & pager_url_last & """>" & previous_page_text & "</a></li>"
        End If
    End Sub

    ' next page
    Public Sub Subnext_page(ByVal next_page_text As String, ByVal pager_url_last As String)
        If current_page < total_pages Then
            next_page = "<li class=""sepaleft""><a href=""" & pager_url & (current_page + 1) & pager_url_last & """>" & next_page_text & "</a></li>"
        End If
    End Sub

    ' first page
    Public Sub Subfirst_page(ByVal first_page_text As String, ByVal pager_url_last As String)
        If current_page > 1 Then
            first_page = "<li class=""separight""><a href=""" & pager_url & "1" & pager_url_last & """>" & first_page_text & "</a></li>"
        End If
    End Sub

    ' last page
    Public Sub Sublast_page(ByVal last_page_text As String, ByVal pager_url_last As String)
        If current_page < total_pages Then
            last_page = "<li class=""sepaleft""><a href=""" & pager_url & total_pages & pager_url_last & """>" & last_page_text & "</a></li>"
        End If
    End Sub

    ' pages functions set
    Public Sub pager_set(ByVal input_pager_url, ByVal input_total_records, ByVal input_scroll_page, _
                         ByVal input_per_page, ByVal input_current_page, ByVal input_inactive_page_tag, _
                         ByVal input_previous_page_text, ByVal input_next_page_text, ByVal input_first_page_text, _
                         ByVal input_last_page_text, ByVal input_pager_url_last)
        pager_url = input_pager_url
        total_records = input_total_records
        scroll_page = input_scroll_page
        per_page = input_per_page
        current_page = input_current_page
        
        Subtotal_pages(pager_url, total_records, scroll_page, per_page, current_page)
        Subpage_links(input_inactive_page_tag, input_pager_url_last, total_records)
        Subprevious_page(input_previous_page_text, input_pager_url_last)
        Subnext_page(input_next_page_text, input_pager_url_last)
        Subfirst_page(input_first_page_text, input_pager_url_last)
        Sublast_page(input_last_page_text, input_pager_url_last)
    End Sub
End Class
